class MigrateSecurityData < ActiveRecord::Migration

  class << self
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Data insert/delete methods
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def up
      add_role_data
      add_acl_data
      add_default_users
    end

    def down
      delete_users
      delete_acl_data
      delete_roles
    end

    private
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      # Private data insert/delete methods
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      def add_role_data
        @@admin = Role.create(:name => "Site Admin", :gather_contact_info => true)
        @@user  = Role.create(:name => "Site User", :gather_contact_info => true)
      end

      def delete_roles
        Role.delete_all
        execute("select setval('roles_id_seq',1,false)")
      end

      def add_acl_data
        add_public_data
        add_common_user_access_data
        add_permissions_data
        add_user_group_management_data
        add_user_configuration_metadata_data
        add_site_admin_account_management_data
        add_site_user_account_management_data
      end

      def delete_acl_data
        SystemMethod.delete_all
        execute("select setval('system_methods_id_seq',1,false)")
        execute("delete from roles_system_objects")
        SystemObject.delete_all
        execute("select setval('system_objects_id_seq',1,false)")
      end

      def add_public_data
        sobj = SystemObject.create(:name => "Public", :can_remove => false)
        @@admin.add_system_object(sobj) 
        @@user.add_system_object(sobj) 

        SystemMethod.create(:name => "home/index", :system_object_id => sobj.id)
        SystemMethod.create(:name => "security/login", :system_object_id => sobj.id)
        SystemMethod.create(:name => "security/logout", :system_object_id => sobj.id)
        SystemMethod.create(:name => "security/denied", :system_object_id => sobj.id)
      end

      def add_common_user_access_data
        sobj = SystemObject.create(:name => "Common User Access", :can_remove => false)
        @@admin.add_system_object(sobj) 
        @@user.add_system_object(sobj) 

        SystemMethod.create(:name => "manage/users/show_my_account", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/update_my_account", :system_object_id => sobj.id)
      end

      def add_permissions_data
        sobj = SystemObject.create(:name => "Permissions")
        @@admin.add_system_object(sobj) 

        SystemMethod.create(:name => "manage/permissions/list_system_objects",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/create_system_object",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/show_system_object",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/update_system_object",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/delete_system_object",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/list_roles",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/update_role",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/show_role",
                            :system_object_id => sobj.id)
      end

      def add_user_group_management_data
        sobj = SystemObject.create(:name => "User Group Management")
        @@admin.add_system_object(sobj) 

        SystemMethod.create(:name => "manage/permissions/list_user_groups",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/create_user_group",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/show_user_group",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/update_user_group",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/permissions/delete_user_group",
                            :system_object_id => sobj.id)
      end

      def add_user_configuration_metadata_data
        sobj = SystemObject.create(:name => "User Configuration Metadata")
        @@admin.add_system_object(sobj) 

        SystemMethod.create(:name => "manage/metadata/contact_formats",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/metadata/user_address_types",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/metadata/user_contact_types",
                            :system_object_id => sobj.id)
      end

      def add_site_admin_account_management_data
        sobj = SystemObject.create(:name => "Site Admin Account Management")
        @@admin.add_system_object(sobj) 

        SystemMethod.create(:name => "manage/users/list_site_admins",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/refine_site_admins_list",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/create_site_admin",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/show_site_admin",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/update_site_admin",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/delete_site_admin",
                            :system_object_id => sobj.id)
      end

      def add_site_user_account_management_data
        sobj = SystemObject.create(:name => "Site User Account Management")
        @@admin.add_system_object(sobj) 

        SystemMethod.create(:name => "manage/users/list_site_users",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/refine_site_users_list",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/create_site_user",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/show_site_user",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/update_site_user",
                            :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/users/delete_site_user",
                            :system_object_id => sobj.id)
      end

      def add_default_users
        Updater.create(:name => "Andrew Stone", :user_id => 1)

        User.create(:first_name => "Andrew", 
                    :last_name => "Stone", 
                    :login => "astone", 
                    :password => Security::Encryption.encrypt('password'),
                    :role_id => 1,
                    :updater_id => 1, 
                    :updated_by => 1)

      end

      def delete_users
        #Using destroy to get contact/address info as well
        User.destroy_all
      end
  end #class block
end
